package com.ipc.app.biz.impl;

import org.apache.commons.lang.time.DateUtils;

import com.ipc.app.biz.InformationBiz;
import com.ipc.app.constants.ExceptionConstants;
import com.ipc.app.constants.InformationStatusConstants;
import com.ipc.app.exception.IPCBusinessException;
import com.ipc.app.model.Information;
import com.ipc.app.model.Paginator;
import com.ipc.app.service.InformationService;

/**
 * @author haijie.zhang
 * 
 */
public class InformationBizImpl implements InformationBiz {

	private InformationService informationService;

	public Paginator<Information> getInformationByCity(String city,
			String informationType, int pageIndex) {
		Paginator<Information> informations = informationService
				.getInformationByCity(city, informationType, pageIndex);
		return informations;

	}

	public Paginator<Information> getInformationBetweenCities(
			String departureCity, String destinationCity,
			String informationType, int pageIndex) {
		Paginator<Information> informations = informationService
				.getInformationBetween2City(departureCity, destinationCity,
						informationType, pageIndex);
		return informations;
	}

	public Information getInformationDetail(String informationId) {
		Information information = informationService
				.getDetailInformation(informationId);
		return information;
	}

	public Paginator<Information> getRoundInformation(String x, String y,
			String informationType, int pageIndex) {
		Double paramX = Double.valueOf(x);
		Double paramY = Double.valueOf(y);
		Double startX = paramX - 0.1;
		Double endX = paramX + 0.1;
		Double startY = paramY - 0.1;
		Double endY = paramY + 0.1;

		Paginator<Information> informations = informationService
				.getRoundInformation(startX, endX, startY, endY,
						informationType, pageIndex);
		return informations;
	}

	public Paginator<Information> getInformationByDepAndDes(String dep_x,
			String dep_y, String des_x, String dex_y, String informationType,
			int pageIndex) {

		Paginator<Information> informations = informationService
				.getInformationByDepAndDes(Double.valueOf(dep_x) - 0.05, Double
						.valueOf(dep_x) + 0.05, Double.valueOf(dep_y) - 0.05,
						Double.valueOf(dep_y) + 0.05,
						Double.valueOf(des_x) - 0.05,
						Double.valueOf(des_x) + 0.05,
						Double.valueOf(dex_y) - 0.05,
						Double.valueOf(dex_y) + 0.05, informationType,
						pageIndex);
		return informations;
	}

	public Paginator<Information> getInformationByUser(String userID,
			String informationType, int pagetIndex) {
		Paginator<Information> informations = informationService
				.getInformationByUserID(userID, informationType, pagetIndex);
		return informations;
	}

	public void informationRelease(Information information) {
		try {
			String status = information.getStatus();
			if (InformationStatusConstants.LONG_EFFECTIVE.equals(status)) {
				DateUtils.addDays(information.getDepartureTime(), 60);
			}

			if (!InformationStatusConstants.isContain(status)) {
				throw new IPCBusinessException(ExceptionConstants.PARAM_ERROR);
			}

			information.setStatus(InformationStatusConstants.EFFECTIVE);
			informationService.informationRelease(information);
		} catch (IPCBusinessException e) {
			// TODO:异常处理
			e.printStackTrace();
		}
	}

	public void setInformationService(InformationService informationService) {
		this.informationService = informationService;
	}

	public void setInformationExpired() {
		informationService
				.setExpiredInformation(InformationStatusConstants.EXPIRED);

	}

}
